package com.yx.yinshanjinniu.activitys;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.PointsMallAdapter;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.bean.IntegralTicketBean;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.constants.Constant;
import com.yx.yinshanjinniu.core.AppAction;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.zc.mychart.model.GoodsAllBean;

import java.util.ArrayList;
import java.util.List;

public class PointsMallActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "PointsMallActivity";
    private RecyclerView recyclerView;
    private PointsMallAdapter pointsMallAdapter;
    private TextView points_integral_text,tv_right,integral_record;
    private ImageView iv_chongzhi,iv_dinggou;

    List<IntegralTicketBean>list = new ArrayList<>();
    @Override
    protected int setContentView() {
        return R.layout.activity_points_mall;
    }

    @Override
    protected void findViews() {
        points_integral_text = (TextView) $(R.id.points_integral_text);
        iv_chongzhi = $(R.id.iv_chongzhi);
        iv_dinggou = $(R.id.iv_dinggou);
        tv_right = $(R.id.tv_right);
        integral_record = $(R.id.integral_record);
        recyclerView = (RecyclerView) findViewById(R.id.points_recycler);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(
                    3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        pointsMallAdapter = new PointsMallAdapter(mContext,list);
        recyclerView.setAdapter(pointsMallAdapter);
        UserInfoBean bean = DataUtils.getUserInfo(mContext);
        points_integral_text.setText("我的积分：" + bean.integral);

        getViewdata();
        pointsMallAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                IntegralTicketBean bean = ( IntegralTicketBean)list.get(position);
                Log.e(TAG, "onItemClick: "+bean.getName() );
                exchangeTicket(bean);
            }
        });
    }
    void exchangeTicket(IntegralTicketBean bean){
        AppAction mAppAction = MyApplication.getApplication().getAppAction();

        mAppAction.exchangeTicket(bean.getId()+"", new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext,"兑换成功");
                finish();
            }
            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);

            }
        });
    }

    @Override
    protected void setListeners() {
        iv_dinggou.setOnClickListener(this);
        iv_chongzhi.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        integral_record.setOnClickListener(this);
    }

    private void getViewdata(){
        AppAction mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.getIntegralTicketListData(new ActionCallbackListener<List<IntegralTicketBean>>() {
            @Override
            public void onSuccess(List<IntegralTicketBean> data) {
                list.addAll(data);
                pointsMallAdapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });
    }

    @Override
    public void onClick(View view) {
        if(view.getId() ==R.id.iv_chongzhi){
            RouteUtil.toRechargeActivity((BaseActivity) mContext);
            return;
        }
        if(view.getId() == R.id.iv_dinggou){
            List<GoodsAllBean> allList = myApplication.getAllList();
            if (null != allList && allList.size() > 0) {
                RouteUtil.toMarketActivity((BaseActivity) mContext, allList.get(0).pro_code);
            } else {
                ToastUtil.showToast(mContext, "暂未获取商品信息");
            }
            return;
        }
        if(view.getId() == R.id.tv_right){
            RouteUtil.toWebViewActivity(this, Constant.H5_POINTS_RULE);
            return;
        }
        if(view.getId() == R.id.integral_record){
            RouteUtil.gotoActivity(mContext,IntegralRecordActivity.class,null);
            return;
        }
    }
}
